#include "../include/plugins.h"

void AddPlugins(char Plugins[]){
    Plugins[0] = '.';
	Plugins[1] = '*';
	Plugins[2] = '\0';
}

bool DetectPlugins(char FirstChar,char Plugins[]){
    unsigned int i;
    for(i = 0;i < strlen(Plugins);i++)
		if (FirstChar == Plugins[i]){
			EnterPlugins(FirstChar);
			return true;
		}
    return false;
}

void DelCurrentChar(){      //custom function
	putchar('\b');
	putchar(0);
	putchar('\b');
}

void DoNothing(){
	;//What the fuck
}

void EnterPlugins(char FuncChar){
	switch (FuncChar){
	case '.':
		printf("Command : ");
		char InputCmd[32];
		fgets(InputCmd,32,stdin);
		InputCmd[strlen(InputCmd) - 1] = 0;
		system(InputCmd);
		break;

	case '*':
		printf("     ->PluginMode\n     b:baidu   d:EnDict   x:DecToHex :  ");
		char func;
		func = _getch();
		switch (func)	{
		case 'b':			//baidu search
			BaiduSearch();
			break;
		case 'd':			//dictionary
			LookOnDict();
			break;
		case 'x':			//convert to hex
			ConvertToHex();
			break;
		default:
			break;
		}

	default:
		break;
    }
}

void AddPlan(){
    FILE * fplan;
    short icontinue = true;
    if ((fplan = fopen(PLAN_CSV_PATH, "r")) == NULL){
		printf("  !:Cannot find plan.csv , now create new one.\n");
		fplan = fopen(PLAN_CSV_PATH, "w");
	}
    fclose(fplan);
	fplan = fopen(PLAN_CSV_PATH, "a");

	while(icontinue){
        char idetail[128];
        memset(idetail,0,128);
        Plans[plancount+1].ptime = (struct time * )malloc(sizeof(struct time));
        printf("\n    ----> Plan time <ex.151001 13:43>: ");
        scanf("%02d%02d%02d-%02d:%02d",&(Plans[plancount+1].ptime->year),&(Plans[plancount+1].ptime->month),&(Plans[plancount+1].ptime->day),&(Plans[plancount+1].ptime->hour),&(Plans[plancount+1].ptime->minute));
        printf("\n      ----> Plan Details : ");
        fflush(stdin);
        scanf("%s",Plans[plancount+1].detail);
        fflush(stdin);
        plancount++;
        fprintf(fplan,"%02d,%02d,%02d,%02d:%02d,%s\n",Plans[plancount].ptime->year,Plans[plancount].ptime->month,Plans[plancount].ptime->day,Plans[plancount].ptime->hour,Plans[plancount].ptime->minute,Plans[plancount].detail);
        printf("\n Continue? y = 1 / n = 0 : ");
        scanf("%hd",&icontinue);
        fflush(stdin);
	}
    fclose(fplan);
}

void LoadPlan(struct Plan Plans[]){
    FILE *fplan;
    if ((fplan = fopen(PLAN_CSV_PATH, "r")) == NULL){
		printf("  !:Cannot find plan.csv , now create new one.\n");
		fplan = fopen(PLAN_CSV_PATH, "w");
	}
    int i;
    fclose(fplan);
	fplan = fopen(PLAN_CSV_PATH, "r");

	int size;
	fseek(fplan,0,SEEK_END);
	size = ftell(fplan);
	fseek(fplan,0,SEEK_SET);
	for (i = 0,plancount = 0; true; i++)
	{
	    Plans[i].ptime = (struct time * )malloc(sizeof(struct time));
		fscanf(fplan, "%02d,%02d,%02d,%02d:%02d,", &(Plans[i].ptime->year),
            &(Plans[i].ptime->month),
            &(Plans[i].ptime->day),
            &(Plans[i].ptime->hour),
            &(Plans[i].ptime->minute));
		fscanf(fplan, "%s", Plans[i].detail);
		memset(Plans[i].warns,0,sizeof(Plans[i].warns)*8);      //reset the warnings
		plancount++;
		if(size - ftell(fplan) < 10)
            break;
	}
	fclose(fplan);
}

void CheckPlan(struct Plan Plans[]){
    GetTm(timeinfo);
    int t;
    for(int i = 0;i < plancount; i++){
        t = ((Plans[i].ptime)->day - timeinfo->tm_mday) * 1440 + ((Plans[i].ptime)->hour  -  timeinfo->tm_hour ) * 60 + (Plans[i].ptime->minute -  timeinfo->tm_min);
        switch(t){
        case 1440:
            if(!Plans[i].warns[0]){
                Message("Tomorrow",Plans[i].detail);
                Plans[i].warns[0] = true;
            }break;
        case 360:
            if(!Plans[i].warns[1]){
                Message("Six-Hour",Plans[i].detail);
                Plans[i].warns[1] = true;
            }break;
        case 120:
            if(!Plans[i].warns[2]){
                Message("Two-Hour",Plans[i].detail);
                Plans[i].warns[2] = true;
            }break;
        case 60:
            if(!Plans[i].warns[3]){
                Message("One-Hour",Plans[i].detail);
                Plans[i].warns[3] = true;
            }break;
        case 20:
           if(!Plans[i].warns[4]){
                Message("Twenty-Minutes",Plans[i].detail);
                Plans[i].warns[4] = true;
            }break;
        case 5:
            if(!Plans[i].warns[5]){
                Message("Five Minutes",Plans[i].detail);
                Plans[i].warns[5] = true;
            }break;
        case 1:
            if(!Plans[i].warns[6]){
                Message("One-Minute!",Plans[i].detail);
                Plans[i].warns[6] = true;
            }break;
        case 0:
            if(!Plans[i].warns[7]){
                Message("Now!",Plans[i].detail);
                Plans[i].warns[7] = true;
            }break;
        default:
            break;
        }
    }

    if((timeinfo->tm_min == starttime->minute) || (timeinfo->tm_min == starttime->minute + 30) || (timeinfo->tm_min == starttime->minute - 30))
        if(ShouldSports) {
            if(!FirstSports) {
                Message("Now!","Sports!");
                ShouldSports = false;
                GetTime(CheckSports);
            }
            FirstSports = false;
            ShouldSports = false;
            GetTime(CheckSports);
        }

    if(!ShouldSports && timeinfo->tm_min != CheckSports->minute)
        ShouldSports = true;


    if(timeinfo->tm_min == starttime->minute)
        if(ShouldDrink){
            if(!FirstDrink){
                Message("Now!","Drink Water!");
                ShouldDrink = false;
                GetTime(CheckDrink);
            }
            FirstDrink = false;
            ShouldDrink = false;
            GetTime(CheckDrink);
        }

    if(!ShouldDrink && timeinfo->tm_min != CheckDrink->minute)
        ShouldDrink = true;
}

void Message(char  time[], char  detail[]){
    char message[260] = {0};
    strcat(message,"start message ");
    strcat(message,detail);
    strcat(message," ");
    strcat(message,time);
    system(message);
}

void ConvertToHex(){
	printf("  To Convert :");
	int d;
	fflush(stdin);
	scanf("%d", &d);
	printf("										  ----> %X", d);
}

void BaiduSearch(){
	char tmpstr[192],tmp[255];
	printf(" Baidu Search :");
	gets(tmpstr);
	memset(tmp, 0, 255);
	strcat(tmp, "start http://www.baidu.com/s?wd=");
	strcat(tmp, tmpstr);
	system(tmp);
}

void LookOnDict(){
	struct EnWord Word[5671];
	FILE *Dict;
	Dict = NULL;
	short WordCount;
	char CheckWord[32];
	printf("Word To Check :");
	fflush(stdin);
	gets(CheckWord);

	if (!InitDictOK(CheckWord[0], &Dict, &WordCount, Word)){
		return;
		fclose(Dict);
	}
	FindDict(Dict, CheckWord, &WordCount, Word);
	fclose(Dict);
}
//Time
void GetTm(){
    time( &nowtime );
    timeinfo = localtime( &nowtime);
    timeinfo->tm_year = timeinfo->tm_year + 1900;
    timeinfo->tm_mon = timeinfo->tm_mon + 1;
}

void GetTime(struct time *ptime){
    time( &nowtime );
    struct tm * tmp;
    tmp = localtime( &nowtime );
    ptime->year = tmp->tm_year + 1900;
    ptime->month = tmp->tm_mon + 1;
    ptime->day = tmp->tm_mday;
    ptime->hour = tmp->tm_hour;
    ptime->minute = tmp->tm_min;
}

void PrintTime(){
    GetTm();
    printf("%02d:%02d:%02d",timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
}
